var React = require("react");
var Collection =require("../collection/Collection.js");

module.exports = React.createClass({


        handlClick:function(e){

         e.preventDefault();

       

         this.props.detailList(e.target.href);

        } ,
        
        
        getCurrentPage:function(url){
            
            
            console.log(url);
            
            
            return url.substring(url.lastIndexOf("_")+1,url.lastIndexOf("."))-0;
            
        },
        
        getUrlByPageNumber: function(page,url){
            
            var url_pre = url.substring(0,url.lastIndexOf("_"));
            var url_last = url.substring(url.lastIndexOf("."));
            
            
            return url_pre + "_" + page + url_last;
            
        },

		render:function(){

            var models = this.props.collection;
            
            var isDisplay= false;
            
            var url ;
            
            var pageHtml =[] ; 
            if(models instanceof Collection){
                
                var pageStats= models.state;
                
                var totalPages = pageStats.totalPages;
                if(totalPages>1){
                    
                    isDisplay=true;
                    
                    url = models.url;
                    
                    var currentPage = this.getCurrentPage(url);
                    
                    if(currentPage>1){
                        
                        pageHtml.push(<li key="pre"><a href={this.getUrlByPageNumber(currentPage-1,url)} onClick={this.handlClick}>Pre</a></li>);
                        
                    }
                    for(var i=1;i<=totalPages;i++){
                        
                         var active = "";
                          if(currentPage==i){
                              
                              active="active";
                          }
                        
                         pageHtml.push(<li key={i} className={active}><a href={this.getUrlByPageNumber(i,url)} onClick={this.handlClick}>{i}</a></li>);
                        
                    }
                    
                      if(currentPage<totalPages){
                        
                        pageHtml.push(<li key="next"><a href={this.getUrlByPageNumber(currentPage+1,url)}  onClick={this.handlClick}>Next</a></li>);
                        
                    }
                    
                }
                
                
                
            }

			return (
			    
                	<div>		    
                	  <nav>
                  <ul className="pagination">
                    {pageHtml}
                  </ul>
                </nav>
            	</div>		    
		)

		}

});
